package com.goatrock.media.thisway.domain.adapter.impl;

import com.goatrock.media.thisway.domain.adapter.ConfigDataAdapter;
import com.goatrock.media.thisway.common.constants.ApplicationConstants;
import com.goatrock.media.thisway.common.model.ApplicationConfig;
import com.goatrock.media.thisway.domain.stub.ApplicationConfig_Stub_v1;
import java.io.FileNotFoundException;
import org.apache.log4j.Logger;
/**
 * Designed for use by MediaMonger
 * @author COLER12
 */
public class ConfigDataAdapterImpl extends BaseAdapterImpl implements ConfigDataAdapter{

    private Logger logger = Logger.getLogger(ConfigDataAdapterImpl.class);

    public ApplicationConfig retrieveConfigData(){
        logger.debug("attempting to load config data");
        ApplicationConfig acDTO;
        try{

            acDTO = (ApplicationConfig) unmarshallXMLByPath(ApplicationConstants.CONFIG_LOC,
                ApplicationConfig.class);
            logger.info("Application Config successfully loaded.");
        }catch(FileNotFoundException e){
            logger.info("Config file not found.  Creating new instance of one.");
            acDTO = ApplicationConfig_Stub_v1.getAppConfigStub();
            this.saveConfigData(ApplicationConstants.CONFIG_LOC, acDTO);
        }
        
        logger.debug("successfully loaded config data");
        return acDTO;
    }

    public void saveConfigData(String fileLocation, Object obj){
        logger.debug("attempting to save config data");
        this.marshallXMLByPath(fileLocation, obj, ApplicationConfig.class);
        logger.debug("successfully saved config data");
    }
    
}
